﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IslandInterloper.Engine
{
    internal class Island:GameClient
    {
        private int id;
        public int ID
        {
            get
            {
                return id;
            }
        }
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }
        private Location position;
        public ILocation Position
        {
            get
            {
                return position;
            }
        }
        private IslandCommodities commodities;
        public IslandCommodities Commodities
        {
            get
            {
                return commodities;
            }
        }
        internal Island(Game theGame,int theId)
            : base(theGame)
        {
            id = theId;
            name = Game.Config.GenerateName();
            position = new Location(Game.Config.GenerateIslandX(), Game.Config.GenerateIslandY());
            commodities = new IslandCommodities(Game);
        }

        internal Island(Game theGame, XElement element)
            : base(theGame)
        {
            FromXElement(element);
        }
        private void FromXElement(XElement element)
        {
            id = Utilities.XElementToInt(element.Element(GameXNames.id));
            name = element.Element(GameXNames.name).Value;
            position = new Location(element.Element(GameXNames.position));
            commodities = new IslandCommodities(Game, element.Element(GameXNames.commodities));
        }
        public override XElement ToXElement(XName xName)
        {
            XElement result = new XElement(xName,
                new XElement(GameXNames.id, id),
                new XElement(GameXNames.name, name),
                position.ToXElement(GameXNames.position),
                commodities.ToXElement(GameXNames.commodities)
                );
            return result;
        }
    }
}
